﻿/*
 * Ext Gantt 1.2
 * Copyright(c) 2009-2011 Mats Bryntse Consulting
 * mats@ext-scheduler.com
 * http://www.ext-scheduler.com/license.html
 *
 */
 
Ext.ns('Sch.gantt');

/**
 * @class Sch.gantt.RowSelectionModel
 * @extends Ext.grid.RowSelectionModel
 * This class provides the basic implementation row selection in a gantt component.
 * @constructor
 * @param {Object} config The object containing the configuration of this model.
 */
Sch.gantt.RowSelectionModel = Ext.extend(Ext.grid.RowSelectionModel, {
    
    initEvents : function() {
        if(!this.grid.enableDragDrop && !this.grid.enableDrag){
            this.grid.on('rowmousedown', this.handleMouseDown, this);
        }
        
        this.rowNav = new Ext.KeyNav(this.grid.getGridEl(), {
            up: this.onKeyPress, 
            down: this.onKeyPress,
            left: this.onLeftRightKeyPress, 
            right: this.onLeftRightKeyPress,
            scope: this
        });

        this.grid.getView().on({
            scope: this,
            refresh: this.onRefresh,
            rowupdated: this.onRowUpdated,
            rowremoved: this.onRemove
        });
    },

    handleMouseDown : Ext.grid.RowSelectionModel.prototype.handleMouseDown.createInterceptor(function(g, rowIndex, e){
        return !!e.getTarget('.x-grid3-locked');
    }),

    onKeyPress : function(e, name){
        var up = name == 'up',
            method = up ? 'selectPrevious' : 'selectNext',
            last;
        if(!e.shiftKey || this.singleSelect){
            this[method](false);
        }else if(this.last !== false && this.lastActive !== false){
            last = this.last;
            var newIndex = up ? this.findPreviousVisibleIndex(this.lastActive - 1) : this.findNextVisibleIndex(this.lastActive + 1);
            if (newIndex >= 0 ) {
                this.selectRange(this.last, newIndex);
                this.grid.getView().focusRow(this.lastActive);
            }
            if(last !== false){
                this.last = last;
            }
        }else{
           this.selectFirstRow();
        }
    },

    onLeftRightKeyPress : function(e, name){
        var taskStore = this.grid.getTaskStore(),
            left = name === 'left';

        if(!e.shiftKey && this.lastActive !== false){
            var t = taskStore.getAt(this.lastActive);
            
            if (!taskStore.isLeafNode(t)) {
                taskStore[left ? 'collapseNode' : 'expandNode'](t);
            }
        }
    },

    findPreviousVisibleIndex : function(index) {
        var store = this.grid.getTaskStore();

        while(index >= 0) {
            if (store.isVisibleNode(store.getAt(index))) {
                return index;
            }
            index--;
        }
        return -1;
    },

    findNextVisibleIndex : function(index) {
        var store = this.grid.getTaskStore(),
            count = store.getCount();
           
        while(index < count) {
            if (store.isVisibleNode(store.getAt(index))) {
                return index;
            }
            index++;
        }
        return -1;
    },

    selectNext : function(keepExisting){
        if(this.hasNext()){
            this.selectRow(this.findNextVisibleIndex(this.last + 1), keepExisting);
            this.grid.getView().focusRow(this.last);
            return true;
        }
        return false;
    },
    
    selectPrevious : function(keepExisting){
        if(this.hasPrevious()){
            this.selectRow(this.findPreviousVisibleIndex(this.last - 1), keepExisting);
            this.grid.getView().focusRow(this.last);
            return true;
        }
        return false;
    },

    acceptsNav : function(row, col, cm){
        return this.grid.isCellEditable(col, row) && Sch.gantt.RowSelectionModel.superclass.acceptsNav.apply(this, arguments);
    }
});